#include <iostream>
#include <string>
#include <fstream>
#include <io.h>
#include "HashTable2.h"
using namespace std;
HashTable2::HashTable2() { //在构造函数中即可加载26个字母的相关文件，将每个文件中的单词有序地写入顺序表中
	FirstInsert();
	cout << "文件已全部加载完成，祝您使用愉快~" << endl;
	system("pause");
}

void HashTable2::FirstInsert() {
	char filename[26][100] = {"E://英汉词典a.txt","E://英汉词典b.txt","E://英汉词典c.txt","E://英汉词典d.txt","E://英汉词典e.txt",
			                          "E://英汉词典f.txt","E://英汉词典g.txt","E://英汉词典h.txt","E://英汉词典i.txt","E://英汉词典j.txt",
			                          "E://英汉词典k.txt","E://英汉词典l.txt","E://英汉词典m.txt","E://英汉词典n.txt","E://英汉词典o.txt",
			                          "E://英汉词典p.txt","E://英汉词典q.txt","E://英汉词典r.txt","E://英汉词典s.txt","E://英汉词典t.txt",
			                          "E://英汉词典u.txt","E://英汉词典v.txt","E://英汉词典w.txt","E://英汉词典x.txt","E://英汉词典y.txt",
			                          "E://英汉词典z.txt"
			                         };
			int i;
			int j;
			int k;
			int count = 0;
			char buffer[300];
			char eng[200];
			ifstream file;
			//新加入：
			word_length[0] = 0;

			for(k = 0; k < 26; k++) {
				file.open(filename[k]);
				if(!file.is_open() {
					cout << "Error opening file" << endl;
					exit(1);
				}
				while(!file.eof()) {
					file.getline(buffer,100);
					for(i=0; buffer[i]!='\0'; i++) {
						if(buffer[i] == ' ') {
							for(j=0; j<i; j++) {
								eng[j] = buffer[j];
							}
							break;
						}
					}
					word[count] = eng;
					mean[count] = buffer;
					count++;
					memset(eng,'\0',sizeof(eng));//清空这个eng[]数组，以便下次继续调用
				}
				//新修改：
				word_length[k+1] = count;
//				cout << word_length[k+1] << endl;
				file.close();
			}
			length = count;
//			cout << "长度：" << length << endl; 
}

//二分查找（前提：折半查找要求线性表必须采用顺序存储结构）
//三个参数分别是：要查找的数，数组中要查找的范围，
//返回值是数组的下标
//low是最左边的元素的边缘，high是最右边的元素的边缘
int HashTable2::HalfSearch(string n,int low,int high) {
	int middle = (low + high) / 2;//中间元素的下标
	if(low > high) {//递归搜索的第一个出口
		return -1;//未找到这个单词，返回-1；
	}
	//开始搜索
	if (word[middle] == n) {
		return middle;//返回的是找到元素的下标
	} else if (word[middle] < n) {
		low = middle + 1;//查找范围变化，中间值以及所有比中间值小的元素都不考虑
		return HalfSearch(n,low,high);//继续递归
	} else if (word[middle] > n) {
		high = middle - 1;//查找范围变化，中间值以及比中间值大的元素都不考虑
		return HalfSearch(n,low,high);//继续递归
	}
}

void HashTable2::WordFind() {
	char choose;
	string w;
	cout << "请输入您想查找的单词：";
	cin >> w;
	if(HalfSearch(w,0,length) == -1) {
		cout << "并未找到您想查找的单词哦，是否将它插入到词典中呢？" << endl;
		cout << "1：确定			2：取消" << endl;
		cin >> choose;
		if(choose == '1') {
			WordInsert();
		} else if(choose == '2') {
			return;
		} else {
			cout << "请输入正确的选项哦！" << endl;
			system("pause");
			return;
		}
	} else {
		cout << "该单词的意思是：" << endl;
		cout << mean[HalfSearch(w,0,length)] << endl;
		system("pause");
		return;
	}
}

void HashTable2::WordInsert() {
	int i;
	int j;
	int k;
	string w;
	string c;
	cout << "请输入您想添加的单词：";
	cin >> w;
	if(HalfSearch(w,0,length) != -1) {
		cout << "该单词已经存在了哦，请勿重复添加" << endl;
		system("pause");
		return;
	}
	cout << "请输入该单词的解释，如：v.吃得多" << endl;
	cin >> c;
	for(i=0; i<length; i++) {
		if (word[i] > w) {
			for(j = length; j > i; j--) {//整体后移一位
				word[j] = word[j-1];
				mean[j] = mean[j-1];
			}
			word[i] = w;
			mean[i] = w + "   " + c;
			length++;//单词总长度加一
			//新修改：
			for(k=(int)w[0]-97+1; k<27; k++) {
				word_length[k]++;//每个开头对应的单词总数的长度加一
//						cout << word_length[k] << endl;
			}
			FileUpdate((int)w[0]-97);//文件更新

			FirstInsert();//为防止错误 重新录入

			cout << "单词添加成功！" << endl;
			system("pause");
			return;
		}
	}
	word[length] = w;
	mean[length] = w +  "   " + c;
	length++;
	//新修改：
	word_length[(int)w[0]-97+1]++;//每个开头对应的单词总数的长度加一
	FileUpdate((int)w[0]-97);

	cout << "单词添加成功！" << endl;
	system("pause");
}

void HashTable2::WordDelete() {
	int i;
	int j;
	int k;
	int temp;
	string w;
	cout << "请输入您想删除的单词：";
	cin >> w;
	temp = HalfSearch(w,0,length);

	if(temp == -1) {
		cout << "并没有找到您想删除的单词哦，请确认输入信息是否有误~" << endl;
		system("pause");
		return;
	}

	for(j = temp; j < length-1; j++) {//将所有单词和解释前移一位
		word[j] = word[j+1];
		mean[j] = mean[j+1];
	}
	length--;//单词总长度减一

	for(k=(int)w[0]-97+1; k<27; k++) {
		word_length[k]--;//每个开头对应的单词总数的长度减一
//				cout << word_length[k] << endl;
	}

	FileUpdate((int)w[0]-97);	//文件更新

	cout << "该单词删除成功！" << endl;
	system("pause");
}

void HashTable2::FileUpdate(int number) {
	int i;
	char filename[26][100] = {"E://英汉词典a.txt","E://英汉词典b.txt","E://英汉词典c.txt","E://英汉词典d.txt","E://英汉词典e.txt",
	                          "E://英汉词典f.txt","E://英汉词典g.txt","E://英汉词典h.txt","E://英汉词典i.txt","E://英汉词典j.txt",
	                          "E://英汉词典k.txt","E://英汉词典l.txt","E://英汉词典m.txt","E://英汉词典n.txt","E://英汉词典o.txt",
	                          "E://英汉词典p.txt","E://英汉词典q.txt","E://英汉词典r.txt","E://英汉词典s.txt","E://英汉词典t.txt",
	                          "E://英汉词典u.txt","E://英汉词典v.txt","E://英汉词典w.txt","E://英汉词典x.txt","E://英汉词典y.txt",
	                          "E://英汉词典z.txt"
	                         };
	ofstream out(filename[number]);
	if (out.is_open()) {
		for(i = word_length[number]; i < word_length[number+1]; i++) {
//					out << mean[i] << "\n";
			if(i == word_length[number+1]-1) {
				out << mean[i];
			} else {
				out << mean[i] << "\n";
			}
		}
	}
	out.close();
//			cout << "词典更新完毕~" << endl;
//			system("pause");
}

void HashTable2::Show() {
	int i;
	for(i = 0; i < length; i++) {
		cout << mean[i] << endl;
	}
	cout << "单词全部遍历完毕~" << endl;
	cout << "长度为：" << length << endl;
	system("pause");
}

void HashTable2::StartShow() {
	system("cls");
	cout << "                     |=======================|" << endl;
	cout << "                     |欢迎使用本英汉双解词典~|" << endl;
	cout << "                     |                       |" << endl;
	cout << "                     |                       |" << endl;
	cout << "                     |      使用说明：       |" << endl;
	cout << "                     |     1:查找单词：      |" << endl;
	cout << "                     |     2:插入单词:       |" << endl;
	cout << "                     |     3:删除单词:       |" << endl;
	cout << "                     |   4:显示所有单词：    |" << endl;
	cout << "                     |       5:退出          |" << endl;
	cout << "                     |                       |" << endl;
	cout << "                     |                       |" << endl;
	cout << "                     |=======================|" << endl;
	cout << "                     |请选择你想进行的操作： |" << endl;
}
